1 // Fig. 21.5: fig21_05.cpp 2 // Demonstrating namespaces. 3 #include 4 using namespace std; // use std namespace 5 6 int myInt = 98; // global variable 7 8 namespace Example { 9 const double PI = 3.14159; 10 const double E = 2.71828; 11 int myInt = 8; 12 void printValues(); 13 14 namespace Inner { // nested namespace 15 enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 }; 16 } 17 } 18 19 namespace { // unnamed namespace 20 double d = 88.22; 21 } 22 23 int main() 24 { 25 // output value d of unnamed namespace 26 cout << "d = " << d; 27 28 // output global variable 29 cout << "\n(global) myInt = " << myInt; 30 31 // output values of Example namespace 32 cout << "\nPI = " << Example::PI << "\nE = " 33 << Example::E << "\nmyInt = " 34 << Example::myInt << "\nFISCAL3 = " 35 << Example::Inner::FISCAL3 << endl; 36 37 Example::printValues(); // invoke printValues function 38 39 return 0; 40 } 41 42 void Example::printValues() 43 { 44 cout << "\n\nIn printValues:\n" << "myInt = " 45 << myInt << "\nPI = " << PI << "\nE = " 46 << E << "\nd = " << d << "\n(global) myInt = " 47 << ::myInt << "\nFISCAL3 = " 48 << Inner::FISCAL3 << endl; 49 } }